package pk.nak.client.activity;

import java.util.ArrayList;

import pk.nak.client.R;
import pk.nak.enums.AppVars;
import pk.nak.requests.CreateUserRequest;
import pk.nak.responses.CreateUserResponse;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SignUpActivity extends Activity {

	/** Called when the activity is first created. */
	
	private CreateUserResponse response = new CreateUserResponse(){

		@Override
		public void onSuccess(String response) {
			// TODO Auto-generated method stub
			super.onSuccess(response);
			AppVars.pdWait.dismiss();
			
			Toast.makeText(getApplicationContext(), this.response, Toast.LENGTH_LONG * 10).show();
			finish();
		}
		
	};
	
	private View.OnClickListener btnSignUp_OnClick = new View.OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			EditText etName = (EditText) findViewById(R.id.etName);
			EditText etUserName = (EditText) findViewById(R.id.etUserName);
			EditText etPass = (EditText) findViewById(R.id.etPassword);
			EditText etEmail = (EditText) findViewById(R.id.etEmail);
			EditText etDOB = (EditText) findViewById(R.id.etDOB);
			Spinner spinnerGender = (Spinner) findViewById(R.id.spinnerGender);

			String onlyname = "";
			String username = "";
			String password = "";
			String email = "";
			String dob = "";
			String gender = "";
			String city = "Lahore";
			String country = "Pakistan";
			
			if ( etName != null ) onlyname = etName.getText().toString();
			if ( etUserName != null ) username = etUserName.getText().toString();
			if ( etPass != null ) password = etPass.getText().toString();
			if ( etEmail != null ) email = etEmail.getText().toString();
			if ( etDOB != null ) dob = etDOB.getText().toString();
			if ( spinnerGender != null ) gender = spinnerGender.getSelectedItem().toString();
			
			CreateUserRequest request = new CreateUserRequest(onlyname, username, password, email, dob, gender, country, city);
			request.doRequest(response);
			AppVars.pdWait = ProgressDialog.show(v.getContext(), "Creating User", "Please Wait...");
		}
	};
	
	private View.OnClickListener btnReset_OnClick = new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			EditText etName = (EditText) findViewById(R.id.etName);
			EditText etUserName = (EditText) findViewById(R.id.etUserName);
			EditText etPass = (EditText) findViewById(R.id.etPassword);
			EditText etEmail = (EditText) findViewById(R.id.etEmail);
			EditText etDOB = (EditText) findViewById(R.id.etDOB);
			Spinner spinnerGender = (Spinner) findViewById(R.id.spinnerGender);
			
			if ( etName != null ) etName.setText("");
			if ( etUserName != null ) etUserName.setText("");
			if ( etPass != null ) etPass.setText("");
			if ( etEmail != null ) etEmail.setText("");
			if ( etDOB != null ) etDOB.setText("");
			if ( spinnerGender != null ) spinnerGender.setSelection(0);
		}
	};
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.signup);
	    this.attachHandlers();
	    // TODO Auto-generated method stub
	}
	
	private void attachHandlers(){
		Button btnSignUp = (Button)findViewById(R.id.btnSignUp);
		Button btnReset = (Button)findViewById(R.id.btnReset);
		
		if ( btnSignUp != null ) btnSignUp.setOnClickListener(btnSignUp_OnClick);
		if ( btnReset != null ) btnReset.setOnClickListener(btnReset_OnClick);
		
		ArrayList<String> arrItems = new ArrayList<String>();
		arrItems.add("");
		arrItems.add("Male");
		arrItems.add("Female");
		
		ArrayAdapter<String> aaItems = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,arrItems);
		Spinner sp = (Spinner)findViewById(R.id.spinnerGender);
		sp.setAdapter(aaItems);
	}
}
